package edu.spec.student.controller;

import edu.spec.student.model.lecture.Lecture;
import edu.spec.student.service.iface.ILectureService;
import edu.spec.basic.model.dto.MvcReturn;
import edu.spec.basic.model.enumeration.Status;
import edu.spec.basic.util.FileTool;
import org.madgecko.basic.model.Pager;
import org.madgecko.basic.model.SystemContext;
import org.madgecko.basic.util.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Date;

/**
 * "讲座管理"控制器
 */
@Controller
@RequestMapping("/admin/lecture")
public class LectureController {

    @Inject
    private ILectureService lectureService;

    @ResponseBody
    @RequestMapping("/add")
    public MvcReturn add(Lecture l){
        try {
            l.setCreateDate(new Date());
            long endTime = l.getStartTime().getTime() + l.getDuration()*60*1000;
            l.setEndTime(new Date(endTime));
            // 将海报从临时区移动到保存区
            String snapshoot = l.getSnapshoot();
//        String contextPath = req.getSession().getServletContext().getRealPath("/");
            String sourcePath = SystemContext.getSystemPath() + "\\resources\\temp\\" + snapshoot;
            String destPath = SystemContext.getSystemPath() + "\\resources\\wechat\\upload\\images\\" + snapshoot;
            File sourceFile = new File(sourcePath);
            Files.copy(sourceFile.toPath(), new File(destPath).toPath());
            sourceFile.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
        lectureService.add(l);
        return new MvcReturn(true);
    }

    /**
     * 用户上传讲座的海报，上传完成后，返回服务器上刚上传的地址
     * @param snapshoot
     * @param req
     * @param resp
     */
    @RequestMapping("/snapshoot")
    public void uploadSnapshoot(MultipartFile snapshoot, HttpServletRequest req, HttpServletResponse resp){
        try {
            // 临时保存，并不能保证用最终会保持这个海报
            MvcReturn re = null;
            String fullPath = FileTool.saveTempFile(snapshoot, req);
            String[] strs = fullPath.split("\\\\");
            int len = strs.length;

            if(len<=0){
                re = new MvcReturn(false);
            }else{
                String path = strs[len-1];
                re = new MvcReturn(true, path, "操作成功");
            }
            resp.setContentType("text/html;charset=UTF-8");
            resp.getWriter().write(JsonUtil.getInstance().obj2json(re));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping("/delete/{id}")
    public MvcReturn delete(@PathVariable Integer id){
        lectureService.delete(id);
        return new MvcReturn(true);
    }

    @ResponseBody
    @RequestMapping("/update")
    public MvcReturn update(Lecture l){
        lectureService.update(l);
        return new MvcReturn(true);
    }

    @ResponseBody
    @RequestMapping("/toggleStatus/{id}")
    public MvcReturn toggleStatus(@PathVariable Integer id){
        lectureService.toggleStatus(id);
        return new MvcReturn(true);
    }

    @ResponseBody
    @RequestMapping("/findByCon")
    public MvcReturn findByCon(Status status, String keyword){
        Pager<Lecture> data = lectureService.findByCon(status, keyword);
        return new MvcReturn(true, data.getDatas(), data.getTotal());
    }

}
